// pages/bind/bind.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nameFocus: true,
    passFocus: false,
    username: '',
    password: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '管理端登录',
    })
  },

  nameInput: function(e) {
    this.setData({
      username: e.detail.value
    })
  },

  passInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  bindSubmit: function() {
    var that = this;
    if (!this.data.username) {
      app.showConfirm('提示', '请输入邮箱或手机号', false, function() {
        that.setData({
          nameFocus: true
        })
      })
    } else if (!this.data.password) {
      app.showConfirm('提示', '请输入密码', false, function() {
        that.setData({
          passFocus: true
        })
      })
    } else {
      wx.showLoading({
        title: '请稍后...',
        mask: true
      })
      var data = {
        point_url: app.server.adminApi.setUsers,
        openid: app.globalData.userInfo.openid,
        username: that.data.username,
        user_pass: that.data.password
      }

      app.server.getConnect(data).then(function(res) {
        console.log(res);
        that.getUserInfo();
        app.globalData.config['selectedTarbarType'] = 'Admin';
        app.utils.initAdminWxappStyle(app, that, ()=>{
          if(app.globalData.config['tabbarType']==='custom'){
            wx.reLaunch({
              url: app.globalData.config.SwitchTabToAdminPageUrl,
            })
          } else {
            wx.switchTab({
              url: app.globalData.config.SwitchTabToAdminPageUrl,
            })
          }
        });
      }, function(res) {
        wx.hideLoading();
        console.log(res);
        app.showConfirm('提示', res.data.msg, false, function() {
          that.setData({
            passFocus: true,
            password: '',
          })
        })
      })
    }
  },

  getUserInfo: function () {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getUsersInfo,
      openid: app.globalData.openid
    }
    app.server.getConnect(data).then(function (res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data;
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
